package cs.android.lang;

import com.google.android.maps.GeoPoint;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.TimeZone;

import static cs.java.lang.Lang.MINUTE;
import static cs.java.lang.Lang.error;
import static cs.java.lang.Lang.exception;
import static cs.java.lang.Lang.info;
import static cs.java.lang.Lang.is;
import static cs.java.lang.Lang.no;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Location;
import cs.android.ActivityView;
import cs.android.http.HttpClient;
import cs.android.http.HttpClientImpl;
import cs.java.event.Event;
import cs.java.event.EventImpl;
import cs.java.lang.Lang;

public class AndroidLang {
	private static Application aplication;

	private static void close(Reader reader) {
		if (is(reader)) try {
			reader.close();
		} catch (IOException e) {
			error(e);
		}
	}

	public static void alert(int stringId) {
		Lang.alert(aplication.getString(stringId));
	}

	public static String asString(InputStream input) {
		final char[] buffer = new char[0x10000];
		StringBuilder out = new StringBuilder();
		Reader reader = null;
		try {
			reader = new InputStreamReader(input, "UTF-8");
			int read;
			do {
				read = reader.read(buffer, 0, buffer.length);
				if (read > 0) out.append(buffer, 0, read);
			} while (read >= 0);
			return out.toString();
		} catch (Exception e) {
			error(e);
		} finally {
			close(reader);
		}
		return "";
	}

	public static <T> Event<T> event() {
		return new EventImpl<T>();
	}

	public static Application getAplication() {
		return aplication;
	}

	public static Drawable getDrawable(ActivityView hasactivity, int drawable) {
		return hasactivity.getActivity().getResources().getDrawable(drawable);
	}

	public static HttpClient http() {
		return new HttpClientImpl();
	}

	public static void setApplication(Application aplication) {
		AndroidLang.aplication = aplication;
	}

	public static void sleep(int delay) {
		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {
			error(e);
		}
	}

	public static long timeFrom(long time) {
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
		info((calendar.getTimeInMillis() - time) / MINUTE);
		return System.currentTimeMillis() - time;
	}

	public static int to1E6(double value) {
		return (int) (value * 1E6);
	}

	public static GeoPoint toGeoPoint(Address value) {
		if (no(value)) return null;
		return new GeoPoint(to1E6(value.getLatitude()), to1E6(value.getLongitude()));
	}

	public static GeoPoint toGeoPoint(Location value) {
		if (no(value)) return null;
		return new GeoPoint(to1E6(value.getLatitude()), to1E6(value.getLongitude()));
	}

	public static String urlEncode(String argument) {
		try {
			return URLEncoder.encode(argument, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw exception(e);
		}
	}
}
